import type { AppProps } from 'next/app'
import initializeStore from '@/libs/store'
import { useRouter } from 'next/dist/client/router'
import { Provider } from 'react-redux'
import Head from "next/head"
import { ConfigProvider } from "antd"
import zhCN from 'antd/lib/locale-provider/zh_CN'
const CreatorApp = (
  { Component, pageProps, initStore }: AppProps & { initStore: any }
) => {
  const store = initializeStore(initStore)
  if (typeof window !== "undefined") {
    const title = document.title
    let time = null
    window.onblur = () => {
      if (!!time) {
        clearTimeout(time)
      }
      document.title = encodeURI(title)
    }
    window.onfocus = () => {
      document.title = "o(*≧▽≦)ツ 又好啦~！"
      time = setTimeout(() => {
        document.title = title;
        time = null
      }, 2000)
    }
  }
  const { asPath } = useRouter()
  return (
    <Provider store={store}>
      <Head>
        <link rel="stylesheet" href="/antd.min.css" />
        <link rel="stylesheet" href="/public.css" />
      </Head>
      <ConfigProvider locale={zhCN}>
        <Component {...pageProps} />
      </ConfigProvider>
    </Provider>
  )
}
CreatorApp.getInitialProps = async ({ ctx, Component }) => {
  const { req } = ctx;
  const initStore: any = {}
  if (!!req) {
    initStore.server = true;
  }
  return {
    pageProps: (!!Component.getInitialProps ?
      await Component.getInitialProps(ctx) : {}),
    initStore
  }
}
export default CreatorApp